using System;
using System.Collections.Generic;
using Headspring.Training.Domain;
using Headspring.Training.Domain.Model;
using Headspring.Training.Domain.Services;
using NHibernate;
using NHibernate.Expression;
using StructureMap;

namespace Headspring.Training.DataAccess.Impl
{
	[Pluggable(Keys.DEFAULT)]
	public class EmployeeRepository : RepositoryBase, IEmployeeRepository
	{
		public Employee GetByUserName(string userName)
		{
			ISession session = getSession();
			ICriteria criteria = session.CreateCriteria(typeof (Employee));
			criteria.SetCacheable(true);
			criteria.Add(Expression.Eq("UserName", userName));
			Employee match = criteria.UniqueResult<Employee>();
			return match;
		}

		public Employee[] GetAllEmployees()
		{
			ISession session = getSession();
			ICriteria criteria = session.CreateCriteria(typeof (Employee));
			criteria.SetCacheable(true);
			IList<Employee> list = criteria.List<Employee>();
			Employee[] employees = new List<Employee>(list).ToArray();
			Array.Sort(employees);
			return employees;
		}
	}
}